home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 109_01.zip / VARTABS.C < prev    next >
Text File  |  1993-06-26  |  5KB  |  196 lines

  1. /*
  2.     Program to copy a file replacing:
  3.         spaces with tabs and spaces 'entab'
  4.         or tabs and spaces with spaces 'detab'
  5.     optional fourth argument allows the tab stop modulus
  6.     to be user specified instead of 8.
  7. */
  8.  
  9. /*
  10.     Macros for constant definitions
  11. */
  12.  
  13. #define ERROR -1    /* error flag returned by buffered i/o routines */
  14. #define EOFF -1        /* end of file marker returned by getc() */
  15. #define EOF 0x1A    /* CP/M's end file char for ascii files */
  16. #define NOFILE -1    /* no such file indication given by fopen() */
  17. #define TAB 0x09    /* tab character */
  18. #define MAXLINE 79    /* max column number in a line */
  19.  
  20. /*
  21.     Argument vector indices
  22. */
  23.  
  24. #define XFER_TYPE 1    /* entab or detab transfer */
  25. #define FROM_FILE 2
  26. #define TO_FILE   3
  27. #define MODULUS   4
  28.  
  29. /*
  30.     main to open the files for entab() or detab()
  31.     and handle invocation errors.
  32. */
  33.  
  34. main(argc,argv)
  35.     int argc;
  36.     char *argv[];
  37.     {
  38.     int fdin,fdout;
  39.     char inbuf[134],outbuf[134];
  40.  
  41.     if(argc != 4 && argc != 5) {
  42.       printf("Correct invocation form is:\n\n");
  43.       printf("VARTABS <detab or entab> <from file> <to file>");
  44.       printf(" [<tab stop modulus>]\n");
  45.       }
  46.     else if( (fdin = fopen(argv[FROM_FILE],inbuf)) == NOFILE )
  47.       printf("No such file %s\n",argv[FROM_FILE]);
  48.     else if( (fdout  = fcreat(argv[TO_FILE],outbuf)) == ERROR )
  49.       printf("Can't open %s\n",argv[TO_FILE]);
  50.     else if( argc == 4) {
  51.       if( tolower(*argv[XFER_TYPE]) == 'e' )
  52.         entab(inbuf,outbuf,8);
  53.       else if( tolower(*argv[XFER_TYPE]) == 'd')
  54.         detab(inbuf,outbuf,8);
  55.       else
  56.         printf("What !!\n");
  57.       }
  58.     else {
  59.       if( tolower(*argv[XFER_TYPE]) == 'e' )
  60.         entab(inbuf,outbuf,atoi(argv[MODULUS]));
  61.       else if( tolower(*argv[XFER_TYPE]) == 'd' )
  62.         detab(inbuf,outbuf,atoi(argv[MODULUS]));
  63.       else
  64.         printf("What !!\n");
  65.       }
  66.     exit();
  67.     }
  68.  
  69. /*
  70.     procedure detab --copy file to file translating tabs to spaces
  71. */
  72.  
  73. detab(filein,fileout,modulo)
  74.     char filein[];    /* the input file buffer */
  75.     char fileout[];    /* the output file buffer */
  76.     int  modulo;        /* modulus for tab stops */
  77.     {
  78.     int col;        /* column counter */
  79.     char tabs[MAXLINE];    /* vector of tab_stops */
  80.     int c;        /* 1 char buffer */
  81.  
  82.     settab(tabs,MAXLINE,modulo);    /* init the tab_stops to mod modulo cols */
  83.     col = 0;
  84.     while( (c = getc(filein)) != EOFF )
  85.       if( c == TAB ) {
  86.         do {
  87.     putchr(' ',fileout);
  88.     col++;
  89.     } while( ! tabpos(col,tabs,MAXLINE) );
  90.         }
  91.       else if( c == '\n' ) {
  92.         putc(c,fileout);
  93.         col = 0;
  94.         }
  95.       else {
  96.         putc(c,fileout);
  97.         col++;
  98.         }
  99.     if( fflush(fileout) == ERROR)
  100.       err_exit("output file flush error");
  101.     return;
  102.     }    /* end detab */
  103.  
  104.  
  105. /*
  106.     procedure entab --copy file to file translating
  107.             spaces to tabs and spaces
  108. */
  109.  
  110. entab(filein,fileout,modulo)
  111.     char filein[];    /* the input file buffer */
  112.     char fileout[];    /* the output file buffer */
  113.     int  modulo;        /* modulus for tab stops */
  114.     {
  115.     int col,newcol;    /* column counters */
  116.     char tabs[MAXLINE];    /* vector of tab_stops */
  117.     int c;        /* 1 char buffer */
  118.  
  119.     settab(tabs,MAXLINE,modulo);    /* init the tab_stops to mod modulo */
  120.     col = 0;
  121.     while(1) {
  122.       newcol = col;
  123.       while( (c = getc(filein)) == ' ' ) { /* collect blanks */
  124.         newcol++;
  125.         if( tabpos(newcol,tabs,MAXLINE) ) {
  126.     putchr(TAB,fileout);
  127.     col = newcol;
  128.     }    /* end if */
  129.         }        /* end while */
  130.       for(; col<newcol; col++)
  131.         putchr(' ',fileout);
  132.       if(c == ERROR)
  133.         err_exit("error in reading file\n");
  134.       putchr(c,fileout);
  135.       if(c == EOF)
  136.         break;
  137.       if( c == '\n' )
  138.         col = 0;
  139.       else
  140.         col++;
  141.       }    /* end while(1) */
  142.     if( fflush(fileout) == ERROR )
  143.       err_exit("error in flushing output buffer\n");
  144.     return;
  145.     }    /* end entab */
  146.  
  147. /*
  148.     procedure -- putchr write a char to an output file and
  149.         error exit with message if error occurrs
  150. */
  151.  
  152. putchr(c,buf)
  153.     char c,buf[];
  154.     {
  155.     int stat;
  156.  
  157.     stat = putc(c,buf);
  158.     if(stat == ERROR) err_exit("error during buffered write\n");
  159.     return;
  160.     }
  161.  
  162.  
  163. /* procedure err_exit -- print error message and exit to CP/M */
  164.  
  165. err_exit(msg)
  166.     char *msg;
  167.     {
  168.     printf("%s",*msg);
  169.     exit();
  170.     }
  171.  
  172. /*
  173. function tabpos -- return true if past end of MAXLINE or at a tab_stop
  174.         false otherwise
  175. */
  176.  
  177. tabpos(col,tabs,max)
  178.     int col,max;
  179.     char tabs[];
  180.     {
  181.     return ( col >max || tabs[col] );
  182.     }
  183.  
  184. /* procedure settab -- initialize the array of tab_stops to mod 8 values */
  185.  
  186. settab(tabs,max,modulo)
  187.     int max,modulo;
  188.     char tabs[];
  189.     {
  190.     int i;
  191.  
  192.     for(i=0; i<=max; i++)
  193.       tabs[i] = (i % modulo == 0);
  194.     return;
  195.     }
  196.